<%@ codepage=65001%><% 'UTF-8%>

<% '会员卡管理%>
<!--#include file="../Include/Common.All.asp"-->
<!--#include file="../Include/Conn.asp"-->
<!--#include file="../Include/CardMake.asp"-->
<% call OpenConn%>

<!--#include file="../Include/Common.Security.asp"-->
<!--#include file="../Include/MD5.asp"-->
<!--#include file="../Include/JSON_2.0.2.asp"-->
<!--#include file="../Include/JSON_UTIL_0.1.1.asp"-->



<%

	'定义json对象
	Dim jsonObj,jsonObj_user
	Set jsonObj = jsObject()
	Set jsonObj_user = jsObject()
	UserID=ReplaceBadChar(Trim(Session("UserID")))
	
	dim act 
  act=request("act")
 	Dim rsObj,strSql
	Dim cardcode ,batch,batch_num,batchcode ,remarks
	Dim  RowCount ''操作所影响的行数
	Dim page ''当前查询页
	Dim jsonObj_map 
	Dim jsonObj_page''返回页面对象
	dim TotalRsNum ,PageSizeCur''TotalRsNum总记录数''PageSizeCur当前页记录数
	''查询信息列表
	if "search"=act then
		cardcode=ReplaceBadChar(Trim(request.Form("cardcode")))
		batch=ReplaceBadChar(Trim(request.Form("batch")))
		batchcode=ReplaceBadChar(Trim(request.Form("batchcode")))
		page=ReplaceBadChar(Trim(request.Form("page")))&""
				
		jsonObj("rslist") = ""
		''添加用户信息
		jsonObj_user("UserID") = UserID 
		jsonObj_user("UserName") =ReplaceBadChar(Trim(Session("UserName")))
		Set jsonObj("customer") = jsonObj_user
	
	 
		''处理key值和value的对应关系 eg:"cardtype":[{"key":"0","value":"\u5931\u6548"},{"key":"1","value":"\u751F\u6548"}]
		''Dim jsonObj_map 
		Set jsonObj_map = jsArray()
		
		set jsonObj_map(0) = jsObject()
		jsonObj_map(0)("key") = "0"
		jsonObj_map(0)("value") = "未开卡"
		set jsonObj_map(1) = jsObject()
		jsonObj_map(1) ("key") = "1"
		jsonObj_map(1) ("value") = "生效"
		set jsonObj_map(2) = jsObject()
		jsonObj_map(2) ("key") = "2"
		jsonObj_map(2) ("value") = "锁定"
		set jsonObj_map(3) = jsObject()
		jsonObj_map(3) ("key") = "3"
		jsonObj_map(3) ("value") = "作废"
	
		Set jsonObj("cardtype") =  jsonObj_map
		''处理key值和value的对应关系--end
	
		If ("" = UserID ) Then UserID = "-999hasd9huoad9e993jwoi982niuh9wncdisj99"
	 
		''添加查询结果信息
		''Dim rsObj,strSql
		strSql = "SELECT top 100 PERCENT KH_XX.id as orderid1,  "
		strSql=strSql & " KH_XX.* FROM KH_XX   WHERE 1=1  "
		If (cardcode <> "") Then  strSql=strSql & " and KH_XX.cardcode like '%" & cardcode & "%'  "
		If (batch <> "") Then  strSql=strSql & " and KH_XX.batch like '%" & batch & "%'  "
		If (batchcode <> "") Then  strSql=strSql & " and KH_XX.batchcode = '" & batchcode & "'  "
		strSql=strSql & " ORDER BY orderid1  "	
		''dim TotalRsNum ,PageSizeCur''TotalRsNum总记录数''PageSizeCur当前页记录数
		strSqlTemp = "select count(*)  from ( " & strSql & " ) kksds "
		TotalRsNum = PE_CLng(Conn.Execute(strSqlTemp)(0))
		''PageSize 页的记录数量
		PageSizeCur = PageSize ''当前页记录数
		if (PageSize*PE_CLng(page)>TotalRsNum or 0=TotalRsNum) then ''判断最后一页记录数是否和PageSize相等
			PageSizeCur= TotalRsNum Mod PageSize ''该页的时间记录数
		end if

		strSqlTemp = " SELECT ttkkk.* FROM (SELECT TOP " & CStr(PageSizeCur)
		strSqlTemp = strSqlTemp & "  mmsss.* FROM (SELECT TOP " & CStr(PageSize*PE_CLng(page)) & " nnfff.* FROM ( " 
		strSqlTemp = strSqlTemp &	strSql  
		strSqlTemp = strSqlTemp &  " ) nnfff) mmsss  ORDER BY mmsss.orderid1 DESC) ttkkk ORDER BY ttkkk.orderid1"
		Set jsonObj("rslist") = QueryToJSON(Conn, strSqlTemp)
		
		''添加分页信息
		'定义json对象
		''Dim jsonObj_page
		Set jsonObj_page = jsObject()
			jsonObj_page("PageCur") = Page ''当前页	
			jsonObj_page("PageSizeCur") = PageSizeCur ''当前页记录数
			if (0=TotalRsNum  mod PageSize  ) then
				jsonObj_page("PageTotalNum")= TotalRsNum\PageSize''总页数
			else
				jsonObj_page("PageTotalNum")= TotalRsNum\PageSize+1''总页数
			end if
		Set jsonObj("page") = jsonObj_page 
	
	elseif "searchbatch"=act then	
		''查询信息列表
		cardcode=ReplaceBadChar(Trim(request.Form("cardcode")))
		batch=ReplaceBadChar(Trim(request.Form("batch")))
		batchcode=ReplaceBadChar(Trim(request.Form("batchcode")))
		page=ReplaceBadChar(Trim(request.Form("page")))&""
				
		jsonObj("rslist") = ""
		''添加用户信息
		jsonObj_user("UserID") = UserID 
		jsonObj_user("UserName") =ReplaceBadChar(Trim(Session("UserName")))
		Set jsonObj("customer") = jsonObj_user
	
	 
		''处理key值和value的对应关系 eg:"cardtype":[{"key":"0","value":"\u5931\u6548"},{"key":"1","value":"\u751F\u6548"}]
		'Dim jsonObj_map 
		Set jsonObj_map = jsArray()
		
		set jsonObj_map(0) = jsObject()
		jsonObj_map(0)("key") = "0"
		jsonObj_map(0)("value") = "未开卡"
		set jsonObj_map(1) = jsObject()
		jsonObj_map(1) ("key") = "1"
		jsonObj_map(1) ("value") = "生效"
		set jsonObj_map(2) = jsObject()
		jsonObj_map(2) ("key") = "2"
		jsonObj_map(2) ("value") = "锁定"
		set jsonObj_map(3) = jsObject()
		jsonObj_map(3) ("key") = "3"
		jsonObj_map(3) ("value") = "作废"
	
		Set jsonObj("cardtype") =  jsonObj_map
		''处理key值和value的对应关系--end
	
		If ("" = UserID ) Then UserID = "-999hasd9huoad9e993jwoi982niuh9wncdisj99"
	 
		''添加查询结果信息
		''Dim rsObj,strSql
		strSql = "SELECT TOP 100 PERCENT batchcode as orderid1 ,batchcode , COUNT(cardcode) AS cardcode_count, batch,   zt  "
		strSql=strSql & "  FROM KH_XX   WHERE 1=1  "
		If (cardcode <> "") Then  strSql=strSql & " and KH_XX.cardcode like '%" & cardcode & "%'  "
		If (batch <> "") Then  strSql=strSql & " and KH_XX.batch like '%" & batch & "%'  "
		If (batchcode <> "") Then  strSql=strSql & " and KH_XX.batchcode = '" & batchcode & "'  "
		strSql=strSql & " GROUP BY zt, batch, batchcode ORDER BY orderid1 "	
		''dim TotalRsNum ,PageSizeCur''TotalRsNum总记录数''PageSizeCur当前页记录数
		strSqlTemp = "select count(*)  from ( " & strSql & " ) kksds "
		TotalRsNum = PE_CLng(Conn.Execute(strSqlTemp)(0))
		''PageSize 页的记录数量
		PageSizeCur = PageSize ''当前页记录数
		if (PageSize*PE_CLng(page)>TotalRsNum or 0=TotalRsNum) then ''判断最后一页记录数是否和PageSize相等
			PageSizeCur= TotalRsNum Mod PageSize ''该页的时间记录数
		end if

		strSqlTemp = " SELECT ttkkk.* FROM (SELECT TOP " & CStr(PageSizeCur)
		strSqlTemp = strSqlTemp & "  mmsss.* FROM (SELECT TOP " & CStr(PageSize*PE_CLng(page)) & " nnfff.* FROM ( " 
		strSqlTemp = strSqlTemp &	strSql  
		strSqlTemp = strSqlTemp &  " ) nnfff) mmsss  ORDER BY mmsss.orderid1 DESC) ttkkk ORDER BY ttkkk.orderid1"
		Set jsonObj("rslist") = QueryToJSON(Conn, strSqlTemp)
		
		''添加分页信息
		'定义json对象
		''Dim jsonObj_page
		Set jsonObj_page = jsObject()
			jsonObj_page("PageCur") = Page ''当前页	
			jsonObj_page("PageSizeCur") = PageSizeCur ''当前页记录数
			if (0=TotalRsNum  mod PageSize  ) then
				jsonObj_page("PageTotalNum")= TotalRsNum\PageSize''总页数
			else
				jsonObj_page("PageTotalNum")= TotalRsNum\PageSize+1''总页数
			end if
		Set jsonObj("page") = jsonObj_page 
			
 elseif "add"=act then
''添加信息
			cardid = ReplaceBadChar(Trim(request.Form("cardid")))
			card_Validate = ReplaceBadChar(Trim(request.Form("card_Validate")))
			batch = ReplaceBadChar(Trim(request.Form("batch"))) 
			remarks = ReplaceBadChar(Trim(request.Form("remarks"))) 
			If (cardid <> "" and card_Validate <> "" and UserID <> "" ) Then
				dim cardid_t
				strSql = "select count(cardcode)as 'aaaaa' from KH_XX where cardcode='"&cardid&"'"
				cardid_t = Conn.Execute(strSql)(0)
				if (0 <> PE_CLng(cardid_t)) Then''卡号冲突
						jsonObj("msg") =  "&#x6DFB;&#x52A0;&#x5931;&#x8D25;&#xFF0C;&#x5361;&#x540D;&#x51B2;&#x7A81;&#xFF01;" '''"添加失败，卡名冲突！" 
						jsonObj("success") = 0'"false"
				else
						''Dim rsObj,strSql
						strSql = "select ISNULL(MAX(id), 0) as 'aaaaa' from KH_XX "
						cardid_num = Conn.Execute(strSql)(0)+1
						set rsObj=Server.CreateObject("ADODB.Recordset")
						strSql = "Select  * from KH_XX where cardcode='"&cardid&"' "
						rsObj.Open strSql, Conn, 1, 3
						If rsObj.BOF And rsObj.EOF Then
							rsObj.AddNew '如果没记录新增，有记录update					
							rsObj("id") = cardid_num
						end If
						rsObj("cardcode") = cardid
						rsObj("yzm") = card_Validate
						rsObj("zt") = 0
						rsObj("batch") =batch
						rsObj("remarks") =remarks
						rsObj("batchcode") =GetRndPassword(10)&""
						rsObj.Update
						rsObj.close
						Set rsObj = Nothing
					 
						jsonObj("msg") =  "&#x6DFB;&#x52A0;&#x6210;&#x529F;&#xFF0C;&#x6B63;&#x5728;&#x8DF3;&#x8F6C;..."'''"添加成功，正在跳转..."
						jsonObj("success") = 1'"success"
						jsonObj("url") = "card_manage.asp" 
				end if
			else
					jsonObj("msg") = "&#x6DFB;&#x52A0;&#x5931;&#x8D25;&#xFF0C;&#x8BF7;&#x6B63;&#x786E;&#x586B;&#x5199;&#x5361;&#x540D;&#x548C;&#x9A8C;&#x8BC1;&#x7801;&#xFF01;"'''"添加失败，请正确填写卡名和验证码！"
					jsonObj("success") = 0'"false"
			End If  
 elseif "delete"=act then
''删除记录
		cardid=ReplaceBadChar(Trim(request.Form("id"))) 
		strSql = "select count(cardcode)as 'aaaaa' from KH_XX where cardcode='"&cardid&"' and zt=0 "
		cardid_num = Conn.Execute(strSql)(0)
		If cardid_num = 0 Then
        jsonObj("success") = 0 'false
         jsonObj("msg") = "记录删除失败,会员卡正在使用。"
		Else			
	    RowCount = 0
	    If (cardid <> "" and UserID <> "" ) Then 
	    	Conn.Execute ("delete from KH_XX where cardcode='"&cardid&"' and zt=0 "), RowCount
	    End If
	    If RowCount = 0 Then
	        jsonObj("success") = 0 'false
	        jsonObj("msg") = "记录删除失败。"
	    Else
	        jsonObj("msg") = "删除成功，正在跳转..."
					jsonObj("success") = 1'"success"
					jsonObj("url") = "card_manage.asp"
	    End If
  	End If

 elseif "deletebatchcode"=act then
''删除记录
		Dim batchcode_list
		batchcode_list=ReplaceBadChar(Trim(request.Form("id"))) 
		''strSql = "select count(cardcode)as 'aaaaa' from KH_XX where cardcode='"&cardid&"' and zt=0 "
		''cardid_num = Conn.Execute(strSql)(0)
		cardid_num=1
		If cardid_num = 0 Then
        jsonObj("success") = 0 'false
        jsonObj("msg") = "记录删除失败,会员卡正在使用。"
		Else			
	    RowCount = 0
	    If (batchcode_list <> ""  ) Then 
	    	strSql="delete from KH_XX where batchcode in ('"&batchcode_list&"') "
	    	Conn.Execute (strSql), RowCount	    	
	    End If

	    If RowCount = 0 Then
	        jsonObj("success") = 0 'false
	        jsonObj("msg") =  "记录删除失败。"
	    Else
	        jsonObj("msg") = "删除成功，正在跳转..."
					jsonObj("success") = 1'"success"
					jsonObj("url") = "card_make.asp"
	    End If
  	End If
 elseif "make"=act then
''批量制卡
		batch=ReplaceBadChar(Trim(request.Form("batch"))) 
		batch_num=PE_CLng(ReplaceBadChar(Trim(request.Form("batch_num"))) )
		if ""<>batch and 0<batch_num then''参数为空
			dim Card_Make_str
			Card_Make_str=Card_Make(batch,batch_num,Conn)
	    If "-1"=Card_Make_str Then
	        jsonObj("success") = 0 'false
	        jsonObj("msg") = "批量制卡失败。"
	    Else
	        jsonObj("msg") = "批量制卡成功，正在跳转..."
					jsonObj("success") = 1'"success"
					jsonObj("url") = "card_make.asp"
	    End If
		else
			jsonObj("success") = 0 'false
			jsonObj("msg") = "批量制卡失败! 请输入批次名称和正确制卡数量"
	  end If
	end If''''删除记录
 

	'输出json对象
	jsonObj.Flush
	''Response.Write strSql&"-----"
 
	call CloseConn

''{
''    rslist : [ { name: "mac", desc: "computer",     
''                   price: 1000, quantity: 100, alert:null },
''                 { name: "ipod", desc: "music player", 
''                   price:  200, quantity: 200, alert:"on sale now!" },
''                 { name: "cinema display", desc: "screen",       
''                   price:  800, quantity: 300, alert:"best deal!" } ],
''    customer : { first: "John", last: "Public", level: "gold" }
''}
%>